package com.atshiyou.controller;

import com.atshiyou.bean.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@SessionAttributes("sessionmap")
public class MyController {
    //    @RequestMapping(method = RequestMethod.GET,value = "/test")
//    public String test1(){
//        System.out.println("test1被执行");
//        return "success"; //返回的是结果页面的名称
//    }
//    @RequestMapping(value = "/test")
//    public String test1(@RequestParam("username") String name, int age,HttpSession session){
//        System.out.println("test1被执行");
//        System.out.println("username:"+name+",age:"+age);
//        session.setAttribute("session",name);
////        System.out.println(5/0);
//        return "success"; //返回的是结果页面的名称
//    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "logout"; //返回的是结果页面的名称
    }
//    @RequestMapping(value = "/test")
//    public String test1(User user) {
//        System.out.println("test1被执行");
//        System.out.println("username:" + user.getUsername() + ",age:" + user.getAge());
//        return "success"; //返回的是结果页面的名称
//    }
    @RequestMapping("/test2")
    public String test2
    (@DateTimeFormat(pattern = "yyyy-MM-dd") Date date, String uname,HttpServletRequest request, ModelMap map, Model model, HttpSession httpSession){
        System.out.println(date);
        request.setAttribute("birthday",date); // 通过request存值转发时显示，重定向时丢失
        map.addAttribute("modelMapKey",date);
        map.addAttribute("mapname",uname);
        model.addAttribute("modelKey",date);
        model.addAttribute("uname",uname);
        return "success.jsp";//这里的跳转页面属于重定向
    }
//    @RequestMapping("/test2")
//    public ModelAndView test2(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date){
//        System.out.println("前台的数据:"+date);
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("success");
//        modelAndView.addObject("modelAndView",date);
//        return modelAndView;
//    }
}
